﻿using System;
using System.Collections;
using System.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.DAL;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using OrganizedIN.BAL.Common;
using System.Net.Mail;
using System.IO;

namespace OrganizedIN.UI
{
    public partial class Authentiction : System.Web.UI.Page
    {
        BLL objbll = new BLL();
        DataAccessLayer Dal = new DataAccessLayer();
        SqlService objservices = new SqlService();
        public string UserId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null)
            {
                LinkButton1.Visible = true;
            }
            UserId = (Request.QueryString["id"] != null) ? Request.QueryString["id"].ToString() : null;
        }

        protected void btnfsubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string Scode = txtscode.Text.Trim();

                DataSet ds = objbll.Authentication();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["Username"].ToString() == UserId && ds.Tables[0].Rows[i]["AuthenticateNo"].ToString() == Scode)
                        {
                            string qry = "delete from User_Authentication where Username='" + UserId + "' and AuthenticateNo=" + Scode + "";
                            SqlHelper.ExecuteNonQuery(objservices.ConnectionString, CommandType.Text, qry);

                            string qry1 = "Update userinformation set Status='1' where Username='" + UserId + "'";
                            SqlHelper.ExecuteNonQuery(objservices.ConnectionString, CommandType.Text, qry1);
                            lblscerr.Text = "You can Login Now.";
                        }
                    }
                }
                else
                {
                    lblscerr.Text = "Your Code is not Valid.";
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Sendmail();
        }

        public void Sendmail()
        {
            string SysMailID, ToMail, MailPassword;
            SysMailID = ConfigurationManager.AppSettings.Get("SystemMailID");
            MailPassword = ConfigurationManager.AppSettings.Get("MailPassword");

            MailMessage objMail = new MailMessage();
            objMail.To.Add(Session["UserID"].ToString());
            objMail.From = new MailAddress(SysMailID);
            objMail.Subject = "Email using Gmail";
            long AuthenticateNo = getRandomNo();
            string text = File.ReadAllText(Server.MapPath("~/AuthenticationTemplate.htm"));
            string Body = text.Replace("##FirstName##", Session["FName"].ToString()).Replace("##UserName##", Session["UserID"].ToString()).Replace("##AuthenticationCode##", AuthenticateNo.ToString()).Replace("##AuthenticationLink##", ConfigurationManager.AppSettings["SiteLink"] + "/UI/Authentiction.aspx?id=" + Session["UserID"].ToString()).Replace("##Logo##", ConfigurationManager.AppSettings["SiteLink"]);

            //string Body = "Hi, this mail is to test sending ,  Please click on belowlink for activate Your account for 60 days trail period. <a href='www.womenartfoundation.com/Test.aspx?Activation='"+ActiveKey;
            objMail.Body = Body;
            objMail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings.Get("SMTP");
            smtp.Credentials = new System.Net.NetworkCredential(SysMailID, MailPassword);
            smtp.EnableSsl = true;
            try
            {
                ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>alert('Please check your email ID for confirmation')</script>");
                Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(objservices.ConnectionString, CommandType.Text, "if exists (select * from User_Authentication where Username = '" + Session["UserID"].ToString() + "') begin Update User_Authentication set AuthenticateNo = " + AuthenticateNo + " where Username = '" + Session["UserID"].ToString() + "'; end else begin insert into User_Authentication values('" + Session["UserID"].ToString() + "'," + AuthenticateNo + "); end");
                smtp.Send(objMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private long getRandomNo()
        {
            BLL registerobj = new BLL();
            Random rndNo = new Random();
            long autheNo = rndNo.Next(1000, 9999);
            DataSet ds = registerobj.Authentication();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (autheNo == Convert.ToInt64(ds.Tables[0].Rows[i]["AuthenticateNo"].ToString()))
                    {
                        getRandomNo();
                    }
                }
            }

            return (autheNo);

        }
    }
}
